package org.cook.utils.html.yyyy;

import java.io.File;
import java.util.concurrent.CountDownLatch;

import org.cook.utils.common.Constants;
import org.cook.utils.common.Utils;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

public class AnalyseThread extends Thread {
	
	private Object lock = new Object();
	private static int counter = 1;
	
	private static final String key1 = "\u80a0";// 肠
	private static final String key2 = "\u80c3";// 胃
	private static final String key3 = "\u80c0\u6c14";
	private static final String regex = key1 + "|" + key2;
	
	private File htmlFile;
	private CountDownLatch latch;
	
	public AnalyseThread(File htmlFile, CountDownLatch latch) {
		this.htmlFile = htmlFile;
		this.latch = latch;
	}
	
	public void run() {
		try {
			HtmlCleaner cleaner = new HtmlCleaner();
			cleaner.getProperties().setCharset(Constants.ENCODING_GB2312);
			
			TagNode node = cleaner.clean(htmlFile);
			TagNode[] bodyNodes = node.getElementsByName("body", false);
			
			for (TagNode body : bodyNodes) {
				Doctor doctor = createDoctor2(body);
				synchronized (lock) {
					if (doctor != null) {
						doctor.setId(counter++);
						KeyworkFilterInFiles.matchedDoctors.add(doctor);
					}
				}
			}
			
		} catch (Exception e) {
			System.err.println("Analyse file[" + htmlFile.getName() + "] error. " + e.getMessage());
		}
		
		latch.countDown();
	}
	
	private Doctor createDoctor(TagNode bodyNode) throws Exception {
		Doctor doctor = null;
		
		Object[] objs = bodyNode.evaluateXPath("div[2]/table/tbody/tr[2]/td/table/tbody/tr[2]/td[2]/table/tbody/tr[3]/td[2]");// zhuanjia
		
		if (objs.length < 1) {
			return doctor;
		}
		
		TagNode descNode = (TagNode) objs[0];
		String skillDesc = subIndexedContent(descNode, 2);
		
		if (firstStepMatch(skillDesc)) {
			TagNode nameNode = (TagNode) bodyNode.evaluateXPath("div[2]/table/tbody/tr[2]/td/table/tbody/tr[2]/td[2]/table/tbody/tr[2]/td")[0];
			TagNode worktimeNode = (TagNode) bodyNode.evaluateXPath("div[2]/table/tbody/tr[2]/td/table/tbody/tr[2]/td[2]/table/tbody/tr[4]/td")[0];
			
			String worktime = worktimeNode.getText().toString().trim().substring(5);
			worktime = worktime.substring(0, worktime.indexOf(25110));// '或' == 25110
			if (secondStepMatch(skillDesc)) worktime = worktime + "  x";
			
			doctor = new Doctor(nameNode.getText().toString().trim(), worktime, skillDesc);
			doctor.setFileName(htmlFile.getName());
		}
		
		return doctor;
	}
	
	private Doctor createDoctor2(TagNode n) throws Exception {
		Doctor doctor = null;

		Object[] objs2 = n.evaluateXPath("div[2]/table/tbody/tr[2]/td/table/tbody/tr[2]/td[2]/table/tbody/tr");
		if (objs2.length != 5) {
			return doctor;
		}
		
		TagNode descNode = (TagNode) objs2[2];
		String skillDesc = subIndexedContent(descNode, 6);
		
		if (firstStepMatch(skillDesc)) {
			String name = subIndexedContent((TagNode) objs2[1], 2);
			String worktime = subIndexedContent((TagNode) objs2[3], 1);
			worktime = worktime.substring(5, worktime.indexOf(25110));// '或' == 25110
			if (secondStepMatch(skillDesc)) worktime = worktime + "  x";
			
			doctor = new Doctor(name, worktime, skillDesc);
		}
		
		return doctor;
	}
	
	private String subIndexedContent(TagNode n, int index) {
		StringBuilder worktimeBuilder = (StringBuilder) n.getText();
		String[] arr = worktimeBuilder.toString().split(Constants.NEW_LINE_WINDOWS);
		
		return arr[index].trim();
	}
	
	private boolean firstStepMatch(String skilledDesc) {
		return Utils.isMatched(skilledDesc, regex);
	}
	
	private boolean secondStepMatch(String skilledDesc) {
//		Utils.isMatched(skilledDesc, key1) && Utils.isMatched(skilledDesc, key2) && Utils.isMatched(skilledDesc, key3)
		return Utils.isMatched(skilledDesc, key3);
	}
	
}
